package fit.deni.pages;

import java.util.Collection;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.ioc.annotations.Inject;

import fit.deni.beans.Clan;
import fit.deni.beans.Zaposleni;
import fit.deni.hibernate.interfaces.ClanService;

public class Customers {

	@Property
	private Collection<Clan> klijenti;
	@Property
	private Clan singleKlijent;
	@Persist(PersistenceConstants.FLASH)
	@Property
	private Clan noviKlijent;
	
	@Property
	@SessionState
	private Zaposleni korisnik;
	private boolean korisnikExists;
	
	@Inject
	private ClanService clanService;

	private Object onActivate() {
		if(!korisnikExists) return Login.class;
		klijenti = clanService.getAll();
		return null;
	}

	private Object onSuccess() {
		clanService.addClient(noviKlijent);
		noviKlijent = new Clan();
		return this;
	}

	@Component
	private EventLink edit, delete;

	public void onDelete(String id) {
		try {
			clanService.deleteClient(clanService.getClientById(Integer.parseInt(id)));
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle delete errors");
		}
	}
	
	public void onEdit(String id) {
		try {
			noviKlijent = clanService.getClientById(Integer.parseInt(id));
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle edit errors");
		}
	}
	
}
